package com.wn.sk.member.core.controller;


import com.wn.common.api.Result;
import com.wn.sk.member.core.dto.UserDto;
import com.wn.sk.member.core.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 会员用户表 前端控制器
 * </p>
 *
 * @author Yellow Docker
 * @since 2022-09-06
 */
@RestController
@RequestMapping("/user")
@Api(tags = "user的微服务")
@Slf4j
public class UserController {

    @Autowired
    private IUserService userService;

    @ApiOperation("测试项目hello 请求...")
    @GetMapping("hello")
    public String hello(){
        return "hello";
    }

    /**
     *  单独的把 String mobile,String password 封装成一个对象
     *  DTO(Data Transfer Object)，用于表示一个数据传输对象
     *
     *
     *  @Validated 方法参数校验的注解
     *
     * @param userDto
     * @return
     */
    @PostMapping("login")
    public Result<String> login(@RequestBody @Validated UserDto userDto) throws Exception {

        //校验
//        if (StringUtils.isEmpty(userDto.getMobile()) || !StringUtils.isEmpty(userDto.getPassword())){
//            return Result.error();
//        }

        log.info("登录传递过来的数据:{}",userDto);

        /**
         *  登录成功返回token
         */
        String token = userService.login(userDto);

        return Result.common(token);
    }
}

